
// 学习辅助技能
// {"序列":1,"类型":"辅助技能"}
// {"序列":1,"类型":"强化技能"}
// @ts-ignore
export async function execute(client_id,message_json) {
    let index = message_json["序列"] - 1;
    let learn_result = false;
    let login_characters = G.Characters.get_current_login_characters(client_id);
    if (message_json["类型"] === "辅助技能") {
        let is_join_bp = _GangDataCache.has(login_characters["BPID"]);
        const bp_skill = ["强壮", "神速"];
        let skill = login_characters["辅助技能"][index];

        if (skill === undefined) {
            // 先判断有没有技能
            ClientUtils.send_basic_prompt(client_id, "#R你没有这样的技能");
        } else if (is_join_bp) {
            //加入帮派
            let gang_data = _GangDataCache.get(login_characters["BPID"])
            // 如果技能等级将要大于所在帮派的技能等级，那么不能学习
            if(skill["等级"] >= gang_data["技能数据"][skill["名称"]]["当前"]){
                ClientUtils.send_basic_prompt(client_id, "#R当前已达帮派技能等级上限");
                return;
            }
            learn_result = learn_skills(client_id,login_characters,index);
        } else {
            //未加入帮派
            if (bp_skill.includes(skill["名称"]) && !is_join_bp) {
                //指定的技能必须加入帮派才能学习
                ClientUtils.send_basic_prompt(client_id, "#R强壮神速需要加入帮派才能进行学习！");
            } else if (skill["等级"] >= 40 && !is_join_bp) {
                //技能等级大于40级之后 必须加入帮派才能学习
                ClientUtils.send_basic_prompt(client_id, "#R你必须先加入一个帮派才可继续学习");
            } else {
                learn_result = learn_skills(client_id,login_characters,index);
            }
        }
        if (learn_result) {
            //通知客户端刷新数据
            ClientUtils.send_client_message(client_id,34, {
                "序列": index + 1,
                "等级": skill["等级"],
                "名称": skill["名称"],
            });
            ClientUtils.send_client_message(client_id,10, G.Characters.computing_characters_data(login_characters));
        }

    }
    if (message_json["类型"] === "强化技能") {
        //学习强化技能
        let learn_result = false;
        let skill = login_characters["强化技能"][index];
        if (skill === undefined || skill === null) {
            ClientUtils.send_basic_prompt(client_id, "#R你没有这样的技能");
        } else {
            let level = skill["等级"] + 1;
            let exp = level * 50000;
            let money = level * 50000;
            let max_level = 160
            if (level > max_level) {
                ClientUtils.send_basic_prompt(client_id, "#R该技能等级已达到上限");
            }
            else if (login_characters["当前经验"] < exp) {
                ClientUtils.send_basic_prompt(client_id, "#R经验不够学习技能");
            }
            else if (login_characters["储备"] >= money) {
                //优先扣除储备
                login_characters["储备"] -= money;
                login_characters["当前经验"] -= exp;
                login_characters["强化技能"][index]["等级"] += 1;
                login_characters["强化技能"][index]["学会"] = true;
                learn_result = true;

            } else if ((login_characters["储备"] + login_characters["银子"]) >= money) {
                //组合扣除储备和银子
                login_characters["银子"] -= (money - login_characters["储备"]);
                login_characters["储备"] = 0;
                login_characters["当前经验"] -= exp;
                login_characters["强化技能"][index]["等级"] += 1;
                login_characters["强化技能"][index]["学会"] = true;
                learn_result = true;
            }
            else {
                ClientUtils.send_basic_prompt(client_id, "#R银子不够学习技能");
            }
            if (learn_result) {
                ClientUtils.send_basic_prompt(client_id, `学习成功,你消耗了${money}银子以及${exp}经验`);
                ClientUtils.send_client_message(client_id, 34.9, {
                    "序列": index + 1,
                    "等级": skill["等级"],
                    "名称": skill["名称"],
                });
                //刷新角色面板数据
                G.Characters.refrush_characters_ui_data(login_characters);
            }
        }
    }
}

//获取辅助技能消耗
function get_learn_skills_data(skill_name, skill_level) {
    const bp_skill = ["强壮", "神速"];
    if (bp_skill.includes(skill_name)) {
        const data = G.GangUtils.join_bp_data[skill_level - 1];
        data["帮贡"] = skill_level;
        data["需求"] = skill_level * 5;
        return data;
    } else {
        return G.GangUtils.basic_data[skill_level];
    }
}

// 学习技能
function learn_skills(client_id,login_characters,index) {
    let skill = login_characters["辅助技能"][index];
    //判断学习的前置条件是否满足

    let data = get_learn_skills_data(skill["名称"], skill["等级"] + 1);
    let result = true;
    if (login_characters["当前经验"] < data["经验"]) {
        result = false;
        ClientUtils.send_basic_prompt(client_id, "#所需经验不足");
    } else if (login_characters["储备"] >= data["金钱"]) {
        //优先扣除储备
        login_characters["储备"] -= data["金钱"];
        login_characters["当前经验"] -= data["经验"];
        login_characters["辅助技能"][index]["等级"] += 1;
        login_characters["辅助技能"][index]["学会"] = true;
    } else if ((login_characters["储备"] + login_characters["银子"]) >= data["金钱"]) {
        //组合扣除储备和银子
        login_characters["银子"] -= (data["金钱"] - login_characters["储备"]);
        login_characters["储备"] = 0;
        login_characters["当前经验"] -= data["经验"];
        login_characters["辅助技能"][index]["等级"] += 1;
        login_characters["辅助技能"][index]["学会"] = true;
    } else {
        result = false;
        ClientUtils.send_basic_prompt(client_id, "#所需银子不足");
    }
    if (result) {
        ClientUtils.send_basic_prompt(client_id, `学习成功,你消耗了${data["金钱"]}银子以及${data["经验"]}经验`);
    }
    return result;
}